<?php
//namespace SP\Database;
class SP_Database_MysqlAbstract
{
	private $host='localhost';
	private $user;
	private $pass;
	private $connect;
	private $dbname;
	private $result;
	private $dbselected;
	private $last_id;
	private $last_query;
	private $escape_query=true;
	var $iterator;
	private $show_error=false;
	
	function __construct($dbname,$user,$pass,$host='localhost')
	{
		$this->host=$host;
		$this->dbname=$dbname;
		$this->user=$user;
		$this->pass=$pass;
		$this->dbselected=false;
	}
	
	public function getConnection()
	{
		return $this->connect;
	}
	
	public function escapeQuery($flag)
	{
		$escape_query=$flag;
	}
	
	public function getBaseName()
	{
		return $this->dbname;
	}
	
	public function setEncoding($enc)
	{
		$this->Query('SET NAMES '.$enc);
	}
	
	public function getLastQuery()
	{
		return $this->last_query;
	}
	
	public function getResult()
	{
		return $this->result;
	}
	
	public function getLastInsertId()
	{
		return $this->last_id;
	}
	
	public function realEscape($str)
	{
		return mysql_real_escape_string($str);
	}
	
	public function reConnect($dbname,$user,$pass)
	{
		$this->closeConnect();
		$this->dbname=$dbname;
		$this->user=$user;
		$this->pass=$pass;
		$this->dbselected=false;
		$this->Connect();		
	}
	
	public function changeUser($user,$pass)
	{
		$this->reConnect($this->dbname,$user,$pass);
	}
	
	public function changeBase($dbname)
	{
		$this->reConnect($dbname,$this->user,$this->pass);
	}
	
	public function Connect()
	{
		if(!$this->connect) $this->connect=@mysql_connect($this->host, $this->user, $this->pass) or die('Can not connect to database server');
		return $this->connect;
	}
	
	public function selectBase()
	{
		if (!$this->Connect()) return false;
		return $this->dbselected=@mysql_select_db($this->dbname, $this->connect) or die('Can not connect to database');
	}
	
	public function showError($show)
	{
		$this->show_error=$show;
	}
	
	public function Query($query)
	{
		$this->selectBase();
		if ($escape_query) $query=$this->realEscape($query);
		$this->last_query=$query;
		$this->result=@mysql_query($query,$this->connect) or die(($this->show_error)? $this->getError() : 'Query error');
		$this->last_id=mysql_insert_id();
		$this->iterator=0;
		return $this->result;
	}
	
	public function fetchAssoc()
	{
		$this->iterator+=1;
		if ($this->result)
		{
			return mysql_fetch_assoc($this->result);
		}
		else
		{
			return NULL;
		}
	}
	
	public function getAssocArray($query)
	{
		$array=array();
		$this->Query($query);
		while($row=$this->fetchAssoc())
		{
			$array[]=$row;
		}
		return $array;
	}
	
	public function getNumRows()
	{
		if ($this->result)
		{
			return mysql_num_rows($this->result);
		}
		else
		{
			return NULL;
		}
	}
	
	public function getAffectedRows()
	{
		if ($this->getConnection())
		{
			return mysql_affected_rows($this->getConnection());
		}
		else
		{
			return NULL;
		}
	}
	
	public function getField($field,$query)
	{
		$query=sprintf($query,$field);
		$this->query($query);
		if(!$this->getResult()) return NULL;
		if($this->getNumRows()==1)
		{
			$row=$this->fetchAssoc();
			return $row[$field];
		}
		else
		{
			while($row=$this->fetchAssoc())
			{
				$mas[]=$row[$field];
			}
			return $mas;
		}
	}
	
	public function getAssocRow($query)
	{
		$this->Query($query);
		return $this->fetchAssoc();
	}
	
	public function getAssocField($query,$field,$id_field='id')
	{
		$array=array();
		$this->Query($query);
		while($row=$this->fetchAssoc())
		{
			$array[$row[$id_field]]=$row[$field];
		}
		return $array;
	}
	
	public function getAssocArrayId($query,$id_field='id')
	{
		$array=array();
		$this->Query($query);
		while($row=$this->fetchAssoc())
		{
			$array[$row[$id_field]]=$row;
		}
		return $array;
	}
	
	public function closeConnect()
	{
		if ($this->connect) mysql_close($this->connect);
	}
	
	static public function staticQuery($query)
	{
		return mysql_query($query);
	}
	
	static public function staticFetchAssoc($result)
	{
		return mysql_fetch_assoc($result);
	}
	
	public function getError()
	{
		return mysql_error();
	}
	
}
?>
